package com.yhd.rabbit.one;

import com.rabbitmq.client.*;
import com.yhd.rabbit.util.ConnectionUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @author 二十
 * @since 2022/1/13 11:53 下午
 */
@Slf4j
public class ConsumerA {

    private static final String QUEUE_NAME = "one_queue";

    public static void main(String[] args) throws Exception {
        //1.获取连接
        Connection conn = ConnectionUtil.getConnection();
        //2.创建通道
        Channel channel = conn.createChannel();
        //3.声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //4.声明队列的消费者
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            //获取消息，并处理，类似于监听器
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body:消息体
                String message = new String(body);
                log.info("message:{}", message);
            }
        };

        //5.监听队列，第二个参数：是否自动ACK
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}
